package com.hundredhealth.rose.delegate;

import android.content.Intent;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.hundredhealth.rose.HealthLabelApp;
import com.hundredhealth.rose.R;
import com.hundredhealth.rose.activity.CustomerDetailActivity;
import com.hundredhealth.rose.activity.CustomerInputActivity;
import com.hundredhealth.rose.model.Customer;
import com.hundredhealth.rose.model.PrintLabel;
import com.hundredhealth.rose.task.TaskPrint;
import com.hundredhealth.rose.util.ExecutionError;
import com.hundredhealth.rose.util.ExecutionListener;
import com.kymjs.frame.view.AppDelegate;

/**
 * Created by huazhong on 29/6/2017.
 */

public class CustomerDetailDelegate extends AppDelegate {

    private TextView mCustomerNoView;
    private EditText mCustomerNameView;
    private EditText mCustomerPhoneView;
    private EditText mCustomerBirthdayView;
    private EditText mCustomerSexView;
    private EditText mCustomerIDNumberView;
    private EditText mFinancialAdvisorNameView;
    private EditText mFinancialAdvisorPhoneView;
    private EditText mSalesAddressView;
    private EditText mPrintCountView;
    private Button mPrintButton;
    private TextView mLabelPrintStatusView;

    private PrintLabel printLabel;

    @Override
    public int getRootLayoutId() {
        return R.layout.activity_customer_print;
    }

    @Override
    public void initWidget() {
        super.initWidget();
        mCustomerNoView = get(R.id.customerNo);
        mCustomerNameView = get(R.id.customerName);
        mCustomerPhoneView = get(R.id.customerMobilePhone);
        mCustomerBirthdayView = get(R.id.birthday);
        mCustomerSexView = get(R.id.sex);
        mCustomerIDNumberView = get(R.id.idNumber);
        mFinancialAdvisorNameView = get(R.id.financialAdvisorName);
        mFinancialAdvisorPhoneView = get(R.id.financialAdvisorPhone);
        mSalesAddressView = get(R.id.salesAddress);
        mLabelPrintStatusView = get(R.id.labelPrintStatusView);
        mPrintCountView = get(R.id.printCount);
        mPrintButton = get(R.id.printButton);
        initViewData();
    }

    public boolean doValidate() {
        if (printLabel == null) {
            return false;
        }

        String countStr = mPrintCountView.getText().toString().trim();
        if (countStr.isEmpty()) {
            return false;
        }

        int printCount = Integer.parseInt(countStr);
        if (printCount < 1) {
            return false;
        }
        return true;
    }

    public void updatePrintCount() {
        int printCount = Integer.parseInt(mPrintCountView.getText().toString());
        printLabel.setPrintCount(printCount);
    }

    public void printLabel() {
        mLabelPrintStatusView.setVisibility(View.VISIBLE);

        HealthLabelApp.executorService.submit(new TaskPrint(printLabel, new ExecutionListener<Boolean>() {
            @Override
            public void executed(final Boolean bPrintResult, ExecutionError error) {
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
//                            final boolean bIsOpened = mLabel.GetIO().IsOpened();
                        if (bPrintResult) {
                            mLabelPrintStatusView.setText(getActivity().getResources().getString(R.string.printsuccess));
                        } else {
                            mLabelPrintStatusView.setText(getActivity().getResources().getString(R.string.printfailed));
                        }
                        final Handler handler = new Handler();
                        Runnable runnable = new Runnable() {
                            @Override
                            public void run() {
                                mLabelPrintStatusView.setVisibility(View.GONE);
                                handler.removeCallbacks(this);
                            }
                        };
                        handler.postDelayed(runnable, 2000);
                    }
                });
            }
        }));
    }

    public void disablePrintButton() {
        mPrintButton.setEnabled(false);
    }

    private void initViewData() {
        Intent intent = getActivity().getIntent();
        if (intent != null) {
            Customer customer = (Customer) intent.getSerializableExtra(CustomerDetailActivity.KEY_CUSTOMER);
            if (customer != null) {
                printLabel = new PrintLabel(customer.getCustomerNo(), customer.getCustomerName(), customer.getCustomerSex(), customer.getCustomerBirthday());
                mCustomerNoView.setText(customer.getCustomerNo());
                mCustomerNameView.setText(customer.getCustomerName());
                mCustomerPhoneView.setText(customer.getCustomerPhone());
                mCustomerBirthdayView.setText(customer.getCustomerBirthday());
                mCustomerSexView.setText(customer.getCustomerSex());
                mCustomerIDNumberView.setText(customer.getCustomerIdNumber());
                mFinancialAdvisorNameView.setText(customer.getFinancialAdvisorName());
                mFinancialAdvisorPhoneView.setText(customer.getFinancialAdvisorPhone());
                mSalesAddressView.setText(customer.getSalesAddress());
            }
        }
        mLabelPrintStatusView.setText(getActivity().getString(R.string.info_label_printing));
        mLabelPrintStatusView.setVisibility(View.GONE);
    }
}
